import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
import { resolve } from "path";

export default defineConfig({
	plugins: [react()],
	server: {
		// 启动后自动打开的页面
		open: "/demo/index.html",
	},
	build: {
		lib: {
			entry: "src/index.tsx",
			name: "GreatJoySwiper",
			fileName: "index",
			formats: ["es", "umd"],
		},
		rollupOptions: {
			external: ["react", "react-dom", "antd-mobile"],
			output: {
				globals: {
					react: "React",
					"react-dom": "ReactDOM",
					"antd-mobile": "AntdMobile",
				},
				assetFileNames: (assetInfo) => {
					if (assetInfo.name === "style.css") return "index.min.css";
					return assetInfo.name || "asset";
				},
			},
		},
		cssCodeSplit: false,
		minify: "terser",
		// 代码混淆
		terserOptions: {
			compress: {
				drop_console: true,
				drop_debugger: true,
			},
			mangle: true,
			format: {
				comments: false,
			},
		},
		target: "es2015",
		outDir: "dist",
		emptyOutDir: false, // 不清空输出目录，保留已生成的类型定义文件
	},
	resolve: {
		alias: {
			"@": resolve(__dirname, "src"),
		},
	},
	css: {
		preprocessorOptions: {
			scss: {
				additionalData: ``,
			},
		},
	},
});
